<?php

class Admin_role_mdl extends MY_Model{
	protected $_table = parent::T_M_ROLE;

	public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	/**
	 * 增加角色菜单权限
	 * @param int $roleID 角色ID
	 * @param array $menuIdList 栏目ID数组
	 */
	public function addMenu2Role($roleID,$menuIdList){
		$map = array();
		$menuIdList = (array)$menuIdList;
		foreach ( $menuIdList as $menuId ){
			$map[] = array(
					'mid'=>$menuId,
					'rid'=>$roleID,
			);
		}
		return $this->db->insert_batch(parent::T_M_ROLE_MENU,$map);
	}
	/**
	 * 删除角色菜单权限
	 * @param int $roleID 角色ID
	 * @param array $menuIdList 栏目ID数组
	 */
	public function deleteMenu2Role($roleID,$menuIdList){
		return $this->db->where('rid',$roleID)
						->where_in('mid',(array)$menuIdList)
						->delete(parent::T_M_ROLE_MENU);
	}
	/**
	 * 彻底删除一个角色
	 * @param int $roleId
	 */
	public function deleteRole($roleId){
		// 删除role表
		$res = $this->db->where('id',$roleId)->delete($this->_table);
		if( $res ){
			// 删除role_menu表
			$this->db->where('rid',$roleId)->delete(parent::T_M_ROLE_MENU);
			// 删除admin_role表
			$this->db->where('rid',$roleId)->delete(parent::T_M_ADMIN_ROLE);
		}
		return $res;
	}
	
	
	
	
	/**
	 * 获取角色的栏目ID列表
	 * @param mixed $where
	 * @param string $select
	 */
	public function getRoleDetail($where='',$select='*'){
		if( !empty($where) ){
			if( is_numeric($where) ){
				$this->db->where('id',$where);
			}else{
				$this->db->where($where);
			}
		}
	
		$role = $this->db->select($select)->from($this->_table)->limit(1)->get()->row_array();
		if( empty($role) ) return FALSE;
		$menuIdList = $this->db->select('mid')
							->where('rid',$role['id'])
							->from(parent::T_M_ROLE_MENU)
							->get()
							->result_array();
		$role['menuIdList'] = array_column($menuIdList,'mid');
		return $role;
	}
	
	
	/**
	 * 获取角色及角色所拥有的栏目列表
	 * @return array
	 */
	public function getRoleMenuList(){
		// 获取角色列表(id,name)
		$roleList   = parent::getList();
		$roleIdList = array_column($roleList, 'id');
		$roleList   = array_combine($roleIdList, $roleList);
		// 获取关联的栏目
		$map = $this->db->select('*')
					->from(parent::T_M_ROLE_MENU)
					->where_in('rid',$roleIdList)
					->get()
					->result_array();
		foreach ($map as $v){
			$roleList[$v['rid']]['menuIdList'][] = $v['mid'];
		}

		return $roleList;
	}
	/**
	 * 获取角色ID与栏目ID的
	 * @param array|int $roleIdList
	 */
	public function getMenuIdListOfRole($roleIdList){
		$roleIdList = (array) $roleIdList;
		if ( isset($roleIdList[1]) ){
			$this->db->where_in('rid',$roleIdList);
		}else{
			$this->db->where('rid',$roleIdList[0]);
		}
		$menuIdList = $this->db->select('mid')->from(parent::T_M_ROLE_MENU)->get()->result_array();
		return array_column($menuIdList, 'mid');
	}
	
	
	
	/**
	 * 检测角色名称是否存在
	 * @param string $roleName
	 * @param int $roleID [可选]不检测的角色ID
	 * @return boolean
	 */
	public function checkRoleNameIsExists($roleName , $roleID = NULL){
		if ( !empty($roleID) ) {
			$this->db->where('id != ',$roleID);
		}
		$query = $this->db->select('name')->from($this->_table)->where('name',$roleName)->limit(1)->get();
		return (bool)$query->num_rows();
	}

}
